package com.ruoyi.yyq.config;

import com.rabbitmq.client.AMQP;
import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MQConfig {
    @Bean   // 消息转换器，定义在配置文件中
    public MessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }
    //定义交换机
    @Bean
    public Exchange fanoutExchange(){
        return ExchangeBuilder.fanoutExchange("fanout_exchange").durable(true).build();
    }
    //定义死信交换机
    @Bean
    public Exchange deadExchange(){
        return ExchangeBuilder.fanoutExchange("dead_exchange").durable(true).build();
    }
    //定义订单队列
    @Bean
    public Queue orderQueue(){
        return QueueBuilder.durable("order_queue")
                .deadLetterExchange("dead_exchange")
                .ttl(60000)
                .build();
    }
    //绑定交换机订单队列
    @Bean
    public Binding orderBinding(){
        return BindingBuilder.bind(orderQueue()).to(fanoutExchange()).with("").noargs();
    }
    //定义救援队列（延迟时间为1分钟）
    @Bean
    public Queue helpQueue(){
        return QueueBuilder.durable("help_queue")
                .deadLetterExchange("dead_exchange")
                .ttl(60000)
                .build();
    }
    //绑定交换机救援队列
    @Bean
    public Binding helpBinding(){
        return BindingBuilder.bind(helpQueue()).to(fanoutExchange()).with("").noargs();
    }
    //定义救援死信队列
    @Bean
    public Queue helpdeadQueue(){
        return QueueBuilder.durable("help_dead_queue").build();
    }
    //绑定救援死信交换机死信队列
    @Bean
    public Binding helpdeadBinding(){
        return BindingBuilder.bind(helpdeadQueue()).to(deadExchange()).with("").noargs();
    }
    //定义订单死信队列
    @Bean
    public Queue orderdeadQueue(){
        return QueueBuilder.durable("order_dead_queue").build();
    }
    //绑定订单死信交换机死信队列
    @Bean
    public Binding orderdeadBinding(){
        return BindingBuilder.bind(orderdeadQueue()).to(deadExchange()).with("").noargs();
    }

}
